<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class jQuickForm_Group_Repeatable extends jQuickForm_Group_Div {

    public function __construct($name = null, $attributes = null, $data = null){
        $this->repeatable_rel = $name;
        parent::__construct($name,$attributes,$data);

        jQuickForm::js()->needJquery();
        jQuickForm::js()->needJqueryUi();
        jQuickForm::js()->addJsInline('
    function recalc_repeatable(rel){
        var wrap = jQuery(".repeatable_wrap[rel=\'"+rel+"\']");
        if(wrap.children(\'.repeatable\').length>1){
            wrap.find(".rpt_remove").removeAttr(\'disabled\').removeClass("disabled");
        } else {
            wrap.find(".rpt_remove").attr(\'disabled\', \'disabled\').addClass("disabled");
        }
        if(wrap.attr("max")){
            if(wrap.attr("max") <=  wrap.children(\'.repeatable\').length){
                wrap.find(".rpt_add").attr(\'disabled\', \'disabled\').addClass("disabled");
            } else {
                wrap.find(".rpt_add").removeAttr(\'disabled\').removeClass("disabled");
            }
        }
        wrap.children(\'.repeatable\').each(function(i){
            //ids
            jQuery(this).find("input[type=text]").each(function(y){
                jQuery(this).attr(\'id\', jQuery(this).attr(\'name\').replace("]","_").replace("[","_")+\'_\'+i);
            });
            if(jQuery(this).next().hasClass(".repeatable")){
                jQuery(this).find(".rpt_down").removeAttr(\'disabled\').removeClass("disabled");
            } else {
                jQuery(this).find(".rpt_down").attr(\'disabled\', \'disabled\').addClass("disabled");
            }
            if(jQuery(this).prev().hasClass(".repeatable")){
                jQuery(this).find(".rpt_up").removeAttr(\'disabled\').removeClass("disabled");
            } else {
                jQuery(this).find(".rpt_up").attr(\'disabled\', \'disabled\').addClass("disabled");
            }
        });

        //поставим плейсхолдеры
        jQuery("input[placeholder]").each(function(){
            jqf_placeholder_blur(jQuery(this));
        });
    }
        ','recalc_repeatable');


        jQuickForm::js()->addJsOnload(
        '
            //включим сортировку перетаскиванием
            jQuery(".repeatable_wrap").sortable();
            jQuery(\'.repeatable\').live(\'mouseover mouseout\', function(event) {
                if (event.type == \'mouseover\') {
                    jQuery(this).addClass(\'repeatable_hover\');
                    jQuery(this).find(\'.rpt_btn\').css("display","inline-block");
                } else {
                    jQuery(this).removeClass(\'repeatable_hover\');
                    jQuery(this).find(\'.rpt_btn\').css("display","none");
                }
            });
            jQuery(".rpt_add").live("click",function(){
                if(jQuery(this).hasClass("disabled")) return false;
                block = jQuery(this).closest(".repeatable");
                clone = block.clone();
                clone.insertAfter(block);
                clone.removeClass(\'repeatable_hover\');
                block.removeClass(\'repeatable_hover\');
                clone.find(\'.rpt_btn\').hide();
                clone.find("input[type=text]").val("");
                /*
                clone.find("input[type=text]").each(){
                    jQuery(this).attr(
                        "name",
                        jQuery(this).attr("name")
                        .replace("[","_")
                        .replace("]","_")
                    );
                });
                */
                recalc_repeatable(block.parent().attr("rel"))
                jQuery(this).blur();
                clone.blur();
            });

            jQuery(".rpt_remove").live("click",function(){
                if(jQuery(this).hasClass("disabled")) return false;
                block = jQuery(this).closest(".repeatable");
                rel = block.parent().attr("rel");
                if(jQuery(".repeatable_wrap[rel=\'"+rel+"\'] > .repeatable").length>1){
                    block.remove();
                    recalc_repeatable(rel)
                }
                jQuery(this).blur();
            });

        ','jqf_repeatable_clone');
    }

    public function render(HTML_QuickForm2_Renderer $renderer){
        jQuickForm::js()->addJsOnload('
        recalc_repeatable("'.$this->getAttribute('rel').'");
        ');

//        $renderer = new HTML_QuickForm2_Renderer_Default();
        $renderer->setTemplateForClass(strtolower(get_class($this)),'
<div {attributes}>
    <qf:label><label class="jqf_group"><qf:required><span class="required">* </span></qf:required>{label}</label></qf:label>
    {content}
    {comment}
</div>');
        $renderer->setElementTemplateForGroupClass(strtolower(get_class($this)),'html_quickform2_element','
    <div class="repeatable">
        <fieldset class="repeatable_row">
            <div class="jqf_row<qf:error> jqf_error</qf:error>">
                <label for="{id}" class="jqf_element"><qf:required><span class="required">* </span></qf:required>{label}</label>
                <span class="jqf_error" id="jqferr-{id}"><qf:error>{error}</qf:error></span>
                <div class="jqf_element">
                    {element}
                    {comment}
                    {example}
                </div>
            </div>
            <input class="rpt_btn rpt_add" value="+" type="button" />
            <input class="rpt_btn rpt_remove" value="-" type="button" />
        </fieldset>
    </div>

        ');
        jQuickForm::css()->addCssInline('
.repeatable fieldset{
    border:1px solid transparent;
    padding: 2px 70px 2px 2px;
    margin:0;
}
.repeatable_row .jqf_row{
    display:inline-block;
    margin:0;
}
.repeatable_wrap	{display: inline-block;margin-bottom: 20px;width:100%}
*+html .repeatable_wrap	{display: inline;margin-bottom: 10px;width:100%}
	.repeatable				{width:100%; curso1r: move;}
    	.repeatable_row				{position: relative; line-height: 1.4em; cursor: move;display: inline-block;}
    		.repeatable_row	input[type=text]	{width: 150px; margin: 3px;}
    		.repeatable_row	select				{width: 150px; margin: 3px;}
    		.repeatable_row .rpt_add, .repeatable .rpt_remove	{position: absolute; top: 4px; width: 20px; height: 20px; padding: 0;}
    		.repeatable_row .rpt_add	{right: 35px;}
    		.repeatable_row .rpt_remove	{right: 10px;}
    		.repeatable_row .rpt_btn	{display:none}
.repeatable_hover{background:#f2f2f2}

        ');
        return parent::render($renderer);
    }


}
?>